From 21603ae812f795b9d98bab623cbafc62a7dbc46f Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:41:17 -0600 Subject: [PATCH] move initialization of OptionInt/Double parameters to constructor (#1364) * move init of OptionInt/Double parms to ctor except for id, which isn't known by formats/filters. Also, move validation of OptionInt/Double strings to Option class, where it can be maintained in parallel with the conversion routines. * init Option id in init_vec, init_filter_vec. also fix -Wclazy-unused-non-trivial-variable. add test of Option error detection. --- arcdist.h | 6 ++-- defs.h | 10 ------- filter_vecs.cc | 11 +++++-- filter_vecs.h | 2 +- garmin.h | 4 +-- garmin_gpi.h | 8 ++--- gdb.h | 4 +-- height.h | 4 +-- interpolate.h | 4 +-- main.cc | 10 +++---- option.cc | 35 ++++++++++++++++++---- option.h | 21 +++++++++---- ozi.h | 4 +-- position.h | 4 +-- radius.h | 12 ++++---- reference/badopt1.txt | 1 + reference/badopt2.txt | 1 + reference/badopt3.txt | 1 + reference/badopt4.txt | 1 + smplrout.h | 6 ++-- testo.d/vecs.test | 18 ++++++++++++ vecs.cc | 68 ++++++++----------------------------------- vecs.h | 6 +--- 23 files changed, 124 insertions(+), 117 deletions(-) create mode 100644 reference/badopt1.txt create mode 100644 reference/badopt2.txt create mode 100644 reference/badopt3.txt create mode 100644 reference/badopt4.txt create mode 100644 testo.d/vecs.test diff --git a/arcdist.h b/arcdist.h index 717b6efca..868b3d7f0 100644 --- a/arcdist.h +++ b/arcdist.h @@ -61,7 +61,7 @@ private: /* Data Members */ double pos_dist{}; - OptionDouble distopt; + OptionDouble distopt{true}; OptionString arcfileopt; OptionBool rteopt; OptionBool trkopt; @@ -71,7 +71,7 @@ private: QVector args = { { - "file", &arcfileopt, "File containing vertices of arc", + "file", &arcfileopt, "File containing vertices of arc", nullptr, ARGTYPE_FILE, ARG_NOMINMAX, nullptr }, { @@ -84,7 +84,7 @@ private: }, { "distance", &distopt, "Maximum distance from arc", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr }, { "exclude", &exclopt, "Exclude points close to the arc", nullptr, diff --git a/defs.h b/defs.h index 34baa610f..879a0f257 100644 --- a/defs.h +++ b/defs.h @@ -866,18 +866,8 @@ extern posn_status tracking_status; * for "groups" of exactly one option. */ #define ARGTYPE_BEGIN_REQ 0x04000000U #define ARGTYPE_END_REQ 0x02000000U -/* For integer conversions specify the base to allow strict error checking and - * proper conversion in Vecs */ -#define ARGTYPE_BASE_10 0x00000000U -#define ARGTYPE_BASE_AUTO 0x00100000U -#define ARGTYPE_BASE_16 0x00200000U - -/* For integer and double conversions is trailing data allowed? - * This allows strict error checking in Vecs */ -#define ARGTYPE_ALLOW_TRAILING_DATA 0x00400000U #define ARGTYPE_TYPEMASK 0x00000fffU -#define ARGTYPE_BASEMASK 0x00300000U #define ARGTYPE_FLAGMASK 0xfffff000U #define ARG_NOMINMAX nullptr, nullptr diff --git a/filter_vecs.cc b/filter_vecs.cc index 5f7a42a96..0741a84b2 100644 --- a/filter_vecs.cc +++ b/filter_vecs.cc @@ -270,11 +270,18 @@ void FilterVecs::free_filter_vec(Filter* flt) Vecs::free_options(flt->get_args()); } -void FilterVecs::init_filter_vec(Filter* flt) +void FilterVecs::init_filter_vec(Filter* flt, const QString& fltname) { QVector* args = flt->get_args(); if (args && !args->isEmpty()) { assert(args->isDetached()); + for (auto& arg : *args) { + if (arg.argval != nullptr) { + arg.argval->reset(); + QString id = QStringLiteral("%1(%2)").arg(fltname, arg.argstring); + arg.argval->init(id); + } + } } } @@ -282,7 +289,7 @@ void FilterVecs::init_filter_vecs() { for (const auto& vec : d_ptr_->filter_vec_list) { if (vec.vec != nullptr) { - init_filter_vec(vec.vec); + init_filter_vec(vec.vec, vec.name); } } } diff --git a/filter_vecs.h b/filter_vecs.h index 7f9b8db73..906aba26e 100644 --- a/filter_vecs.h +++ b/filter_vecs.h @@ -70,7 +70,7 @@ public: static void prepare_filter(const fltinfo_t& fltdata); fltinfo_t find_filter_vec(const QString& fltargstring); static void free_filter_vec(Filter* flt); - static void init_filter_vec(Filter* flt); + static void init_filter_vec(Filter* flt, const QString& fltname); void init_filter_vecs(); static void exit_filter_vec(Filter* flt); void exit_filter_vecs(); diff --git a/garmin.h b/garmin.h index 0be8a5ff1..4d503226c 100644 --- a/garmin.h +++ b/garmin.h @@ -128,7 +128,7 @@ private: OptionBool snwhiteopt; OptionString deficon; OptionInt categoryopt; - OptionInt categorybitsopt; + OptionInt categorybitsopt{false, 0}; OptionInt baudopt; OptionString opt_codec; int baud = 0; @@ -171,7 +171,7 @@ private: }, { "bitscategory", &categorybitsopt, "Bitmap of categories", - nullptr, ARGTYPE_BASE_AUTO | ARGTYPE_INT, "1", "65535", nullptr + nullptr, ARGTYPE_INT, "1", "65535", nullptr }, { "baud", &baudopt, "Speed in bits per second of serial port (baud=9600)", diff --git a/garmin_gpi.h b/garmin_gpi.h index 8b380c836..b8ddd9587 100644 --- a/garmin_gpi.h +++ b/garmin_gpi.h @@ -318,8 +318,8 @@ private: OptionBool opt_unique; OptionBool opt_alerts; OptionString opt_units; - OptionDouble opt_speed; - OptionDouble opt_proximity; + OptionDouble opt_speed{true}; + OptionDouble opt_proximity{true}; OptionInt opt_sleep; OptionString opt_lang; OptionString opt_writecodec; @@ -357,7 +357,7 @@ private: }, { "proximity", &opt_proximity, "Default proximity", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr }, { "sleep", &opt_sleep, "After output job done sleep n second(s)", @@ -365,7 +365,7 @@ private: }, { "speed", &opt_speed, "Default speed", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_STRING, ARG_NOMINMAX, nullptr }, { "unique", &opt_unique, "Create unique waypoint names (default = yes)", diff --git a/gdb.h b/gdb.h index a2bcb9680..37025f79e 100644 --- a/gdb.h +++ b/gdb.h @@ -166,7 +166,7 @@ private: OptionInt gdb_opt_ver; OptionBool gdb_opt_via; OptionBool gdb_opt_roadbook; - OptionInt gdb_opt_bitcategory; + OptionInt gdb_opt_bitcategory{false, 0}; OptionBool gdb_opt_drop_hidden_wpt; int waypt_flag{}; @@ -187,7 +187,7 @@ private: }, { "bitscategory", &gdb_opt_bitcategory, "Bitmap of categories", - nullptr, ARGTYPE_BASE_AUTO | ARGTYPE_INT, "1", "65535", nullptr + nullptr, ARGTYPE_INT, "1", "65535", nullptr }, { "ver", &gdb_opt_ver, diff --git a/height.h b/height.h index 9aedb58eb..ab5817461 100644 --- a/height.h +++ b/height.h @@ -47,7 +47,7 @@ public: void process() override; private: - OptionDouble addopt; + OptionDouble addopt{true}; OptionBool wgs84tomslopt; double addf{}; // include static constexpr data member definitions with intializers for grid as private members. @@ -56,7 +56,7 @@ private: QVector args = { { "add", &addopt, "Adds a constant value to every altitude", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_BEGIN_REQ | ARGTYPE_STRING, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_BEGIN_REQ | ARGTYPE_STRING, ARG_NOMINMAX, nullptr }, { "wgs84tomsl", &wgs84tomslopt, "Converts WGS84 ellipsoidal height to orthometric height (MSL)", diff --git a/interpolate.h b/interpolate.h index a0fd4cafd..62d518fa6 100644 --- a/interpolate.h +++ b/interpolate.h @@ -51,7 +51,7 @@ private: OptionDouble opt_time; double max_time_step{0}; - OptionDouble opt_dist; + OptionDouble opt_dist{true}; double max_dist_step{0}; OptionBool opt_route; @@ -63,7 +63,7 @@ private: }, { "distance", &opt_dist, "Distance interval", - nullptr, ARGTYPE_END_EXCL | ARGTYPE_END_REQ | ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING, + nullptr, ARGTYPE_END_EXCL | ARGTYPE_END_REQ | ARGTYPE_STRING, ARG_NOMINMAX, nullptr }, { diff --git a/main.cc b/main.cc index 41c5019c6..426ea549d 100644 --- a/main.cc +++ b/main.cc @@ -245,7 +245,7 @@ run_reader(Vecs::fmtinfo_t& ivecs, const QString& fname) start_session(ivecs.fmtname, fname); if (ivecs.isDynamic()) { ivecs.fmt = ivecs.factory(fname); - Vecs::init_vec(ivecs.fmt); + Vecs::init_vec(ivecs.fmt, ivecs.fmtname); Vecs::prepare_format(ivecs); ivecs->rd_init(fname); @@ -277,7 +277,7 @@ run_writer(Vecs::fmtinfo_t& ovecs, const QString& ofname) } if (ovecs.isDynamic()) { ovecs.fmt = ovecs.factory(ofname); - Vecs::init_vec(ovecs.fmt); + Vecs::init_vec(ovecs.fmt, ovecs.fmtname); Vecs::prepare_format(ovecs); ovecs->wr_init(ofname); @@ -462,7 +462,7 @@ run(const char* prog_name) } if (filter.isDynamic()) { filter.flt = filter.factory(); - FilterVecs::init_filter_vec(filter.flt); + FilterVecs::init_filter_vec(filter.flt, filter.fltname); FilterVecs::prepare_filter(filter); filter->init(); @@ -650,11 +650,11 @@ run(const char* prog_name) if (ivecs.isDynamic()) { ivecs.fmt = ivecs.factory(fname); - Vecs::init_vec(ivecs.fmt); + Vecs::init_vec(ivecs.fmt, ivecs.fmtname); } if (ovecs && ovecs.isDynamic()) { ovecs.fmt = ovecs.factory(ofname); - Vecs::init_vec(ovecs.fmt); + Vecs::init_vec(ovecs.fmt, ovecs.fmtname); } start_session(ivecs.fmtname, fname); diff --git a/option.cc b/option.cc index 8234bf0aa..282fcfcb7 100644 --- a/option.cc +++ b/option.cc @@ -53,11 +53,9 @@ double OptionString::toDouble(bool* ok, QString* end) const return parse_double(value_, id_, ok, end); } -void OptionInt::init(const QString& id, bool allow_trailing_data, int base) +void OptionInt::init(const QString& id) { id_ = id; - base_ = base; - allow_trailing_data_ = allow_trailing_data; } void OptionInt::reset() @@ -77,6 +75,15 @@ void OptionInt::set(const QString& s) result_ = parse_integer(value_, id_, dieonerror, endp, base_); } +bool OptionInt::isValid(const QString& s) const +{ + bool ok; + QString end; + QString* endp = allow_trailing_data_? &end : nullptr; + (void) parse_integer(s, id_, &ok, endp, base_); + return ok; +} + int OptionInt::get_result(QString* end) const { if (end != nullptr) { @@ -85,10 +92,14 @@ int OptionInt::get_result(QString* end) const return result_; } -void OptionDouble::init(const QString& id, bool allow_trailing_data, int /* base */) +bool OptionInt::trailing_data_allowed() const +{ + return allow_trailing_data_; +} + +void OptionDouble::init(const QString& id) { id_ = id; - allow_trailing_data_ = allow_trailing_data; } void OptionDouble::reset() @@ -108,6 +119,15 @@ void OptionDouble::set(const QString& s) result_ = parse_double(value_, id_, dieonerror, endp); } +bool OptionDouble::isValid(const QString& s) const +{ + bool ok; + QString end; + QString* endp = allow_trailing_data_? &end : nullptr; + (void) parse_double(s, id_, &ok, endp); + return ok; +} + double OptionDouble::get_result(QString* end) const { if (end != nullptr) { @@ -115,3 +135,8 @@ double OptionDouble::get_result(QString* end) const } return result_; } + +bool OptionDouble::trailing_data_allowed() const +{ + return allow_trailing_data_; +} diff --git a/option.h b/option.h index f627dab65..b2f69b131 100644 --- a/option.h +++ b/option.h @@ -43,7 +43,7 @@ public: [[nodiscard]] virtual bool has_value() const = 0; [[nodiscard]] virtual bool isEmpty() const = 0; [[nodiscard]] virtual const QString& get() const = 0; - virtual void init(const QString& id, bool allow_trailing_data, int base) {} + virtual void init(const QString& id) {} virtual void reset() = 0; virtual void set(const QString& s) = 0; @@ -83,7 +83,7 @@ public: return value_; } - void init(const QString& id, bool /* allow_trailing_data */, int /* base */) override + void init(const QString& id) override { id_ = id; } @@ -116,6 +116,10 @@ class OptionInt : public Option public: /* Special Member Functions */ OptionInt() = default; + explicit OptionInt(bool allow_trailing_data, int base) : + allow_trailing_data_(allow_trailing_data), + base_(base) + {} explicit(false) operator const QString& () const { @@ -142,18 +146,20 @@ public: return value_; } - void init(const QString& id, bool allow_trailing_data, int base) override; + void init(const QString& id) override; void reset() override; void set(const QString& s) override; + bool isValid(const QString& s) const; int get_result(QString* end = nullptr) const; + bool trailing_data_allowed() const; private: QString value_; QString id_; int result_{}; QString end_; - int base_{10}; bool allow_trailing_data_{false}; + int base_{10}; }; class OptionDouble : public Option @@ -161,6 +167,9 @@ class OptionDouble : public Option public: /* Special Member Functions */ OptionDouble() = default; + explicit OptionDouble(bool allow_trailing_data) : + allow_trailing_data_(allow_trailing_data) + {} explicit(false) operator const QString& () const { @@ -187,10 +196,12 @@ public: return value_; } - void init(const QString& id, bool allow_trailing_data, int /* base */) override; + void init(const QString& id) override; void reset() override; void set(const QString& s) override; + bool isValid(const QString& s) const; double get_result(QString* end = nullptr) const; + bool trailing_data_allowed() const; private: QString value_; diff --git a/ozi.h b/ozi.h index 4f20a2245..d24b423f3 100644 --- a/ozi.h +++ b/ozi.h @@ -134,7 +134,7 @@ private: OptionString wptbgcolor; OptionBool pack_opt; int datum{}; - OptionDouble proximityarg; + OptionDouble proximityarg{true}; double proximity{}; OptionString altunit_opt; OptionString proxunit_opt; @@ -175,7 +175,7 @@ private: }, { "proximity", &proximityarg, "Proximity distance", - "0", ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING, ARG_NOMINMAX, nullptr + "0", ARGTYPE_STRING, ARG_NOMINMAX, nullptr }, { "altunit", &altunit_opt, "Unit used in altitude values", diff --git a/position.h b/position.h index 98b8e384e..67df7f9c0 100644 --- a/position.h +++ b/position.h @@ -64,7 +64,7 @@ private: double pos_dist{}; qint64 max_diff_time{}; - OptionDouble distopt; + OptionDouble distopt{true}; OptionDouble timeopt; OptionBool purge_duplicates; bool check_time{}; @@ -72,7 +72,7 @@ private: QVector args = { { "distance", &distopt, "Maximum positional distance", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr }, { "all", &purge_duplicates, diff --git a/radius.h b/radius.h index 7f83d8929..96f1c67c3 100644 --- a/radius.h +++ b/radius.h @@ -55,7 +55,7 @@ private: /* Data Members */ double pos_dist{}; - OptionDouble distopt; + OptionDouble distopt{true}; OptionDouble latopt; OptionDouble lonopt; OptionBool exclopt; @@ -68,23 +68,23 @@ private: QVector args = { { - "lat", &latopt, "Latitude for center point (D.DDDDD)", + "lat", &latopt, "Latitude for center point (D.DDDDD)", nullptr, ARGTYPE_FLOAT | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr }, { - "lon", &lonopt, "Longitude for center point (D.DDDDD)", + "lon", &lonopt, "Longitude for center point (D.DDDDD)", nullptr, ARGTYPE_FLOAT | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr }, { "distance", &distopt, "Maximum distance from center", - nullptr, ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr + nullptr, ARGTYPE_STRING | ARGTYPE_REQUIRED, ARG_NOMINMAX, nullptr }, { - "exclude", &exclopt, "Exclude points close to center", + "exclude", &exclopt, "Exclude points close to center", nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr }, { - "nosort", &nosort, "Inhibit sort by distance to center", + "nosort", &nosort, "Inhibit sort by distance to center", nullptr, ARGTYPE_BOOL, ARG_NOMINMAX, nullptr }, { diff --git a/reference/badopt1.txt b/reference/badopt1.txt new file mode 100644 index 000000000..7fe10b9cc --- /dev/null +++ b/reference/badopt1.txt @@ -0,0 +1 @@ +radius(lat): conversion to double failed: conversion of "44x" failed due to unexpected trailing data "x". diff --git a/reference/badopt2.txt b/reference/badopt2.txt new file mode 100644 index 000000000..964922217 --- /dev/null +++ b/reference/badopt2.txt @@ -0,0 +1 @@ +stack(depth): conversion to integer failed: invalid argument "b". diff --git a/reference/badopt3.txt b/reference/badopt3.txt new file mode 100644 index 000000000..63c3995fa --- /dev/null +++ b/reference/badopt3.txt @@ -0,0 +1 @@ +gdb(bitscategory): conversion to integer failed: conversion of "0x2z" failed due to unexpected trailing data "z". diff --git a/reference/badopt4.txt b/reference/badopt4.txt new file mode 100644 index 000000000..ea153596b --- /dev/null +++ b/reference/badopt4.txt @@ -0,0 +1 @@ +csv(snlen): conversion to integer failed: invalid argument "a". diff --git a/smplrout.h b/smplrout.h index 5852b03ab..fbfaa6d36 100644 --- a/smplrout.h +++ b/smplrout.h @@ -120,19 +120,19 @@ private: metric_t metric{metric_t::crosstrack}; OptionInt countopt; - OptionDouble erroropt; + OptionDouble erroropt{true}; OptionBool xteopt; OptionBool lenopt; OptionBool relopt; QVector args = { { - "count", &countopt, "Maximum number of points in route", + "count", &countopt, "Maximum number of points in route", nullptr, ARGTYPE_INT | ARGTYPE_BEGIN_REQ | ARGTYPE_BEGIN_EXCL, "1", nullptr, nullptr }, { "error", &erroropt, "Maximum error", nullptr, - ARGTYPE_ALLOW_TRAILING_DATA | ARGTYPE_STRING | ARGTYPE_END_REQ | ARGTYPE_END_EXCL, "0", nullptr, nullptr + ARGTYPE_STRING | ARGTYPE_END_REQ | ARGTYPE_END_EXCL, "0", nullptr, nullptr }, { "crosstrack", &xteopt, "Use cross-track error (default)", nullptr, diff --git a/testo.d/vecs.test b/testo.d/vecs.test new file mode 100644 index 000000000..a805c074d --- /dev/null +++ b/testo.d/vecs.test @@ -0,0 +1,18 @@ +# test detection of invalid option values +# this should cover static and dynamic filters and functions. +${VALGRIND} "${PNAME}" -i geo -f ${REFERENCE}/geocaching.loc -x radius,lat=44x -o gpx -F /dev/null 2> ${TMPDIR}/badopt1.txt && { + echo "${PNAME} succeeded! (it shouldn't have with this input...)" +} +compare ${REFERENCE}/badopt1.txt ${TMPDIR}/badopt1.txt +${VALGRIND} "${PNAME}" -i geo -f ${REFERENCE}/geocaching.loc -x stack,depth=b -o gpx -F /dev/null 2> ${TMPDIR}/badopt2.txt && { + echo "${PNAME} succeeded! (it shouldn't have with this input...)" +} +compare ${REFERENCE}/badopt2.txt ${TMPDIR}/badopt2.txt +${VALGRIND} "${PNAME}" -i gdb,bitscategory=0x2z -f ${REFERENCE}/gdb-sample.gdb -o xcsv -F /dev/null 2> ${TMPDIR}/badopt3.txt && { + echo "${PNAME} succeeded! (it shouldn't have with this input...)" +} +compare ${REFERENCE}/badopt3.txt ${TMPDIR}/badopt3.txt +${VALGRIND} "${PNAME}" -i gdb,bitscategory=0x2 -f ${REFERENCE}/gdb-sample.gdb -o csv,snlen=a -F /dev/null 2> ${TMPDIR}/badopt4.txt && { + echo "${PNAME} succeeded! (it shouldn't have with this input...)" +} +compare ${REFERENCE}/badopt4.txt ${TMPDIR}/badopt4.txt diff --git a/vecs.cc b/vecs.cc index eb6867420..6b07eaa60 100644 --- a/vecs.cc +++ b/vecs.cc @@ -500,7 +500,7 @@ Vecs& Vecs::Instance() * default initializer the default constructor would be implicitly deleted. */ -void Vecs::init_vec(Format* fmt) +void Vecs::init_vec(Format* fmt, const QString& fmtname) { QVector* args = fmt->get_args(); if (args && !args->isEmpty()) { @@ -508,6 +508,8 @@ void Vecs::init_vec(Format* fmt) for (auto& arg : *args) { if (arg.argval != nullptr) { arg.argval->reset(); + QString id = QStringLiteral("%1(%2)").arg(fmtname, arg.argstring); + arg.argval->init(id); } } } @@ -517,54 +519,12 @@ void Vecs::init_vecs() { for (const auto& vec : d_ptr_->vec_list) { if (vec.vec != nullptr) { - init_vec(vec.vec); + init_vec(vec.vec, vec.name); } } style_list = create_style_vec(); } -int Vecs::integer_base(uint32_t argtype) -{ - int base; - switch (argtype & ARGTYPE_BASEMASK) { - case ARGTYPE_BASE_AUTO: - base = 0; - break; - case ARGTYPE_BASE_16: - base = 16; - break; - case ARGTYPE_BASE_10: - default: - base = 10; - } - return base; - -} - -bool Vecs::trailing_data_allowed(uint32_t argtype) -{ - return (argtype & ARGTYPE_ALLOW_TRAILING_DATA) == ARGTYPE_ALLOW_TRAILING_DATA; -} - -bool Vecs::is_integer(const QString& val, const QString& id, uint32_t argtype) -{ - bool ok; - int base = integer_base(argtype); - QString end; - QString* endp = trailing_data_allowed(argtype) ? &end : nullptr; - (void) parse_integer(val, id, &ok, endp, base); - return ok; -} - -bool Vecs::is_float(const QString& val, const QString& id, uint32_t argtype) -{ - bool ok; - QString end; - QString* endp = trailing_data_allowed(argtype) ? &end : nullptr; - (void) parse_double(val, id, &ok, endp); - return ok; -} - bool Vecs::is_bool(const QString& val) { return val.startsWith('y', Qt::CaseInsensitive) || @@ -610,9 +570,6 @@ void Vecs::assign_option(const QString& module, arglist_t& arg, const QString& v } arg.argval->reset(); - int base = integer_base(arg.argtype); - bool allow_trailing_data = trailing_data_allowed(arg.argtype); - arg.argval->init(id, allow_trailing_data, base); if (val.isNull()) { return; @@ -1104,13 +1061,12 @@ bool Vecs::validate_args(const QString& name, const QVector* args) } #endif for (const auto& arg : *args) { - QString id = QStringLiteral("%1(%2)").arg(name, arg.argstring); if (arg.argval == nullptr) { Warning() << name << "option" << arg.argstring << "does not point to an Option instance."; ok = false; } if (const auto* int_option = dynamic_cast(arg.argval); int_option != nullptr) { - if (trailing_data_allowed(arg.argtype)) { + if (int_option->trailing_data_allowed()) { // GUI QIntValidator will reject input with trailing data. if ((arg.argtype & ARGTYPE_TYPEMASK) != ARGTYPE_STRING) { Warning() << name << "OptionInt with trailing data" << arg.argstring << "is not of ARGTYPE_STRING."; @@ -1123,20 +1079,20 @@ bool Vecs::validate_args(const QString& name, const QVector* args) } } - if (!arg.defaultvalue.isNull() && !is_integer(arg.defaultvalue, id, arg.argtype)) { + if (!arg.defaultvalue.isNull() && !int_option->isValid(arg.defaultvalue)) { Warning() << name << "Int option" << arg.argstring << "default value" << arg.defaultvalue << "is not an integer."; ok = false; } - if (!arg.minvalue.isNull() && !is_integer(arg.minvalue, id, arg.argtype)) { + if (!arg.minvalue.isNull() && !int_option->isValid(arg.minvalue)) { Warning() << name << "Int option" << arg.argstring << "minimum value" << arg.minvalue << "is not an integer."; ok = false; } - if (!arg.maxvalue.isNull() && !is_integer(arg.maxvalue, id, arg.argtype)) { + if (!arg.maxvalue.isNull() && !int_option->isValid(arg.maxvalue)) { Warning() << name << "Int option" << arg.argstring << "maximum value" << arg.maxvalue << "is not an integer."; ok = false; } } else if (const auto* double_option = dynamic_cast(arg.argval); double_option != nullptr) { - if (trailing_data_allowed(arg.argtype)) { + if (double_option->trailing_data_allowed()) { // GUI QDoubleValidator will reject input with trailing data. if ((arg.argtype & ARGTYPE_TYPEMASK) != ARGTYPE_STRING) { Warning() << name << "OptionDouble with trailing data" << arg.argstring << "is not of ARGTYPE_STRING."; @@ -1149,15 +1105,15 @@ bool Vecs::validate_args(const QString& name, const QVector* args) } } - if (!arg.defaultvalue.isNull() && !is_float(arg.defaultvalue, id, arg.argtype)) { + if (!arg.defaultvalue.isNull() && !double_option->isValid(arg.defaultvalue)) { Warning() << name << "Float option" << arg.argstring << "default value" << arg.defaultvalue << "is not an float."; ok = false; } - if (!arg.minvalue.isNull() && !is_float(arg.minvalue, id, arg.argtype)) { + if (!arg.minvalue.isNull() && !double_option->isValid(arg.minvalue)) { Warning() << name << "Float option" << arg.argstring << "minimum value" << arg.minvalue << "is not an float."; ok = false; } - if (!arg.maxvalue.isNull() && !is_float(arg.maxvalue, id, arg.argtype)) { + if (!arg.maxvalue.isNull() && !double_option->isValid(arg.maxvalue)) { Warning() << name << "Float option" << arg.argstring << "maximum value" << arg.maxvalue << "is not an float."; ok = false; } diff --git a/vecs.h b/vecs.h index 43676137b..034ef3863 100644 --- a/vecs.h +++ b/vecs.h @@ -71,7 +71,7 @@ public: /* Member Functions */ - static void init_vec(Format* fmt); + static void init_vec(Format* fmt, const QString& fmtname); void init_vecs(); static void free_options(QVector* args); static void exit_vec(Format* fmt); @@ -143,10 +143,6 @@ private: /* Member Functions */ - static int integer_base(uint32_t argtype); - static bool trailing_data_allowed(uint32_t argtype); - static bool is_integer(const QString& val, const QString& id, uint32_t argtype); - static bool is_float(const QString& val, const QString& id, uint32_t argtype); static bool is_bool(const QString& val); static QVector create_style_vec(); QVector sort_and_unify_vecs() const; -- 2.30.2